package com.herohead.backup;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Tuple;

import com.herohead.Misc;
import com.herohead.redis.GetRedisServer;
import com.herohead.redis.RedisKeys;
import com.herohead.redis.RedisVars;

public class CloneStats {

	static Jedis jd;


	public static String getServer (String hash) {

		String temp = "";
		Map<String, String> mp = jd.hgetAll(hash);

		Iterator it = mp.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry pairs = (Map.Entry)it.next();
			//System.out.println(pairs.getKey() + " = " + pairs.getValue());
			temp = temp + pairs.getValue().toString() + "\n";
			//size = size + Long.parseLong(temp);
		}

		//Misc m = new Misc();

		//String s = m.bytesToMB(String.valueOf(size)) + " MB";

		return temp;
	}


	@SuppressWarnings("rawtypes")
	public static String getSize (String hash) {

		long size = 0;

		Map<String, String> mp = jd.hgetAll(hash);

		Iterator it = mp.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry pairs = (Map.Entry)it.next();
			//System.out.println(pairs.getKey() + " = " + pairs.getValue());
			String temp = pairs.getValue().toString();
			size = size + Long.parseLong(temp);
		}

		Misc m = new Misc();

		String s = m.bytesToMB(String.valueOf(size)) + " MB";

		return s;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Misc m = new Misc();

		GetRedisServer grs = new GetRedisServer();

		//String redisHostName = grs.getRedisHost();
		RedisVars.redisHost = grs.getRedisHost();
		//RedisVars.redisHost = "172.21.5.41";


		jd = new Jedis(RedisVars.redisHost,6380);

		try {
			if (!grs.checkForRedis(jd)) {
				System.out.println("redis server not found");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		while (true) {
			jd.connect();
			System.out.println("Threads running : \t" + jd.llen(RedisKeys.listProdThreadRunning));
			System.out.println("Threads queued : \t" + jd.hlen(RedisKeys.hashProdThreadQueuedProcessID));
			System.out.println("Threads completed : \t" + jd.llen(RedisKeys.listProdThreadCompleted));
			System.out.println("Threads failed : \t" + jd.llen(RedisKeys.listProdThreadFailed));
			//System.out.println("Clones running : \t" + jd.llen("prod:clones:running"));
			System.out.println("----");
			System.out.println("Clones running : \t" + jd.llen(RedisKeys.listProdCloneRunning));
			System.out.println("Clones completed : \t" + jd.llen(RedisKeys.lProdCloneCompleted));
			System.out.println("Total Queued size : \t" + getSize(RedisKeys.hashProdThreadQueuedProcessID));
			System.out.println("Total running size : \t" + getSize(RedisKeys.hashProdThreadRunningProcessID));
			System.out.println("Total processed size : \t" + getSize(RedisKeys.listProdThreadCompletedProcessID));
			System.out.println("SSID not cloned : \t" + jd.scard(RedisKeys.setProdSSIDNotCloned));
			System.out.println("Groups not cloned : \t" + jd.scard(RedisKeys.setProdGroupNotCloned));
			System.out.println("Size not cloned : \t" + m.bytesToMB(jd.get(RedisKeys.strProdSizeNotCloned)) + " MB");
			//createTempTable(RedisKeys.hashProdThreadRunningProcessID);
			System.out.println("Smallest running : \t" + m.bytesToMB(getSmallestInHash(RedisKeys.hashProdThreadRunningProcessID)) + " MB");
			System.out.println("Largest running : \t" + m.bytesToMB(getLargestInHash(RedisKeys.hashProdThreadRunningProcessID)) + " MB");
			//System.out.println("Smallest running : \t" + (getSmallestInHash(RedisKeys.hashProdThreadRunningProcessID)) + " MB");
			//System.out.println("Largest running : \t" + (getLargestInHash(RedisKeys.hashProdThreadRunningProcessID)) + " MB");
			//jd.del("temp:hash:list");
			//createTempTable(RedisKeys.hashProdThreadQueuedProcessID);
			System.out.println("Smallest in Queue : \t" + m.bytesToMB(getSmallestInHash(RedisKeys.hashProdThreadQueuedProcessID)) + " MB");
			System.out.println("Largest in Queue : \t" + m.bytesToMB(getLargestInHash(RedisKeys.hashProdThreadQueuedProcessID)) + " MB");
			//System.out.println("Smallest in Queue : \t" + (getSmallestInHash(RedisKeys.hashProdThreadQueuedProcessID)) + " MB");
			//System.out.println("Largest in Queue : \t" + (getLargestInHash(RedisKeys.hashProdThreadQueuedProcessID)) + " MB");
			//jd.del("temp:hash:list");
			//System.out.println("Clones Names : \n" + getServer("prod:clones:running:name"));
			System.out.println("\n-----Clones Names");
			System.out.println(getServer(RedisKeys.hashProdCloneRunningName));
			System.out.println("-----Queue Sizes");
			getQueSizing();
			System.out.println("-----------------------------------");


			jd.close();

			try {
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}


	}

	@SuppressWarnings("null")
	public static String getSmallestInHash(String hash) {



		TreeSet<Long> set = new TreeSet<Long>();

		double d = 0;
		List<String>  s = jd.hvals(hash);
		for (String ss : s) {
			set.add(Long.parseLong(ss));
			//System.out.println(ss);
		}
		if (set.size() == 0) {
			return "0";
		} else {
			return set.first().toString();
		}
		//System.out.println(set.first());
		//String sss = String.valueOf(d);

	}


	public static String getLargestInHash(String hash) {

		TreeSet<Long> set = new TreeSet<Long>();

		double d = 0;
		List<String>  s = jd.hvals(hash);
		for (String ss : s) {
			set.add(Long.parseLong(ss));
			//System.out.println(ss);
		}
		if (set.size() == 0) {
			return "0";
		} else {
			return set.last().toString();
		}
		//System.out.println(set.last());
		//String sss = String.valueOf(d);

	}


	public static void getQueSizing() {

		GetRedisServer grs = new GetRedisServer();

		RedisVars.redisHost = grs.getRedisHost();
		//RedisVars.redisHost = "172.21.5.41";
		RedisVars.redisHost = grs.getHostName();
		Jedis jdro = new Jedis(RedisVars.redisHost,6380);
		Jedis jd = new Jedis(RedisVars.redisHost);
		//System.out.println("Started");
		Set<String> s = jdro.hkeys(RedisKeys.hashProdThreadQueuedProcessID);



		//List<String> runningClones = jdro.hvals(RedisKeys.hashProdCloneRunningName);
		jd.del("z_temp:inprocess:queued:list");
		jd.del("z_temp:inprocess:running:list");
		jd.del("z_temp:inprocess:totalsize");

		for (String processID : s) {

			//System.out.println(processID);
			String command = (jdro.hget(RedisKeys.hashProdThreadQueuedCommand, processID));
			command = command.replace("|", ":");
			//System.out.println(command);
			String cloneName = command.split(":")[0];

			//System.out.println(jdro.hget(RedisKeys.hashProdThreadQueuedProcessID,processID));
			jd.zincrby("z_temp:inprocess:queued:list",1, cloneName);
			jd.zincrby("z_temp:inprocess:totalsize",Double.parseDouble(jdro.hget(RedisKeys.hashProdThreadQueuedProcessID,processID)), cloneName);

		}
		/**
	for (String processID : s) {

		//System.out.println(processID);
		String command = (jdro.hget(RedisKeys.hashProdThreadRunningProcessID, processID));
		command = command.replace("|", ":");
		//System.out.println(command);
		String cloneName = command.split(":")[0];

		//System.out.println(jdro.hget(RedisKeys.hashProdCloneSSID,processID));
		jd.zincrby("z_temp:inprocess:running:list",1, cloneName);

	}

		 **/


		Set<Tuple> tup = jd.zrangeWithScores("z_temp:inprocess:queued:list", 0, -1);
		//Set<Tuple> tupTotal = jd.zrangeWithScores("z_temp:inprocess:totalsize", 0, -1);

		for (Tuple s1 : tup) {
			System.out.print(s1.getElement() + "\t");
			System.out.print(String.format("%.0f",s1.getScore()) + "\t");
			System.out.println(String.format("%.0f",jd.zscore("z_temp:inprocess:totalsize", s1.getElement())/1024/1024));
			//System.out.println(tupTotal     .s1.getElement());
		}

		/**

	for (Tuple s1 : tupTotal) {
		System.out.print("Clone Name : " + s1.getElement() + " ");
		System.out.println(s1.getScore()/1024/1024);
	}

		 **/



		jdro.close();
		jd.close();
	}

}
